import { loginAPI, userInfoAPI } from '@/api/user'

const state = {
  token: '',
  userInfo: {}
}
const mutations = {
  setToken(state, token) {
    state.token = token
  },
  delToken(state) {
    state.token = ''
  },
  setUserInfo(state, data) {
    state.userInfo = data
  },
  delUserInfo(state) {
    state.userInfo = {}
  }
}
const actions = {
  async login(context, data) {
    const res = await loginAPI(data)
    if (res.code === 0) {
      context.commit('setToken', res.data.token.token)
    }
  },
  async getUserInfo(context) {
    const res = await userInfoAPI()
    context.commit('setUserInfo', res.data.data)
  },
  logout(context) {
    context.commit('delToken')
    context.commit('delUserInfo')
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

